package com.example.day6;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MyPagerAdapter extends PagerAdapter {
    private ArrayList<View> list;//数据
    private Context context;
    public MyPagerAdapter(ArrayList<View> list, Context context) {
        this.list = list;
        this.context = context;
    }
    //TODO 返回数量
    @Override
    public int getCount() {
        return list.size();
    }
    //TODO 判断view是否来自集合中
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view==o;
    }
    //TODO 根据下标加载View
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //向容器中添加View
        Log.i("aaaaa", position+"加载了: ");
//        View view= LayoutInflater.from(context).inflate(R.layout.item_layout,null);
//        //在这里可以做相应的操作
//        TextView tv= (TextView) view.findViewById(R.id.text12);
//        //数据填充
////        tv.setText(datas[position]);
        View view= list.get(position);
        container.addView(view);    //这一步很重要
        return view;

//        container.addView(list.get(position));
//        return list.get(position);
    }
    //TODO 根据下标移除:不是移除集合中的,而是移除Viewgroup里面的
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //       super.destroyItem(container, position, object);//注意：该方法不要写
        //从容器中移除View
        Log.i("aaaaa", position+"移除了: ");
        container.removeView(list.get(position));

    }
}

